package org.viettan.android.main.activities.radio;

import java.util.List;

import org.viettan.android.main.activities.common.RefreshableListActivity;
import org.viettan.android.main.intents.IntentKeys;
import org.viettan.android.main.io.DownloadEntityTask;
import org.viettan.android.main.io.EntityRetrievalException;
import org.viettan.android.main.managers.DialogManager;
import org.viettan.android.main.managers.RadioManager;
import org.viettan.android.main.managers.SettingsManager;
import org.viettan.android.main.model.Language;
import org.viettan.android.main.model.RadioProgram;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.inject.Inject;

/**
 * The {@code RadioActivity} downloads the list of RCTM programs and episodes and then displays the
 * list of {@link RadioProgram} (days) in a list to drill into.
 */
public class RadioActivity extends RefreshableListActivity {
    @Inject
    private DialogManager notificationManager;
    @Inject
    private RadioManager radioManager;
    @Inject
    private SettingsManager settingsManager;

    private Language lastLanguage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	loadList(true);
    }

    @Override
    protected void loadList(boolean useCache) {
	lastLanguage = settingsManager.getArticleLanguage();
	new DownloadRadioProgramsTask(this, notificationManager, useCache).execute();
    }

    @Override
    protected void onRestart() {
	super.onRestart();
	if (settingsManager.hasLanguageChanged(lastLanguage)) {
	    loadList(true);
	}
    }

    private void showRadioPrograms(List<RadioProgram> radioPrograms) {
	ArrayAdapter<RadioProgram> adapter = new ArrayAdapter<RadioProgram>(this,
		android.R.layout.simple_list_item_1, radioPrograms);

	setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView listView, View view, int position, long id) {
	RadioProgram radioProgram = (RadioProgram) getListAdapter().getItem(position);
	Intent intent = new Intent(this, RadioProgramActivity.class);
	intent.putExtra(IntentKeys.RADIO_EPISODES.name(), radioProgram);
	startActivity(intent);
    }

    /**
     * A helper class to download the list of programs from Viet Tan's website in a background
     * thread.
     */
    private static class DownloadRadioProgramsTask extends
	    DownloadEntityTask<RadioActivity, List<RadioProgram>> {
	private final boolean useCache;

	private DownloadRadioProgramsTask(RadioActivity radioActivity,
		DialogManager notificationManager, boolean useCache) {
	    super(radioActivity, notificationManager);
	    this.useCache = useCache;
	}

	@Override
	protected List<RadioProgram> getEntity() throws EntityRetrievalException {
	    RadioActivity activity = getActivity();
	    if (activity == null) {
		return null;
	    }
	    return activity.radioManager.getAllPrograms(useCache);
	}

	@Override
	protected void displayEntity(List<RadioProgram> articles) {
	    RadioActivity activity = getActivity();
	    if (activity == null) {
		return;
	    }
	    activity.showRadioPrograms(articles);
	}
    }
}
